/**
 * Created by Administrator on 2018/4/28.
 */
/**
 * Created by Administrator on 2018/4/26.
 */
define(function (require,exports,module) {
    var sTpl = require("./template.html");
    require("./serviceBill.css");
    require("../../lib/fullcalendar/moment.min.js");
    var VueComponent = Vue.extend({
        template: sTpl
        ,methods:{
            formatTime:function () {
                if(!this.normal.timeDiff){
                    this.startDate = this.currentDate.format("YYYY-MM-DD");
                    var next =  this.currentDate.clone();
                    next.day(next.day()+1);
                    this.endDate = next.format("YYYY-MM-DD");
                }else{
                    this.startDate = this.currentDate.format("YYYY-MM-DD");
                    var next =  this.currentDate.clone();
                    next.week(next.week()+1);
                    this.endDate = next.format("YYYY-MM-DD");
                }
            },
            today:function () {
                if(!this.normal.timeDiff){
                    this.currentDate =  new moment;
                }else{
                    this.currentDate =  moment().startOf('week');
                }
                this.formatTime()
                this.query();
            },
            before:function (t) {
                if(!this.normal.timeDiff){
                    this.currentDate.day(this.currentDate.day()-1)
                }else{
                    this.currentDate.week(this.currentDate.week()-1)
                }
                this.formatTime()
                this.query();
            },
            after:function (t) {
                if(!this.normal.timeDiff){
                    this.currentDate.day(this.currentDate.day()+1)
                }else{
                    this.currentDate.week(this.currentDate.week()+1)
                }
                this.formatTime()
                this.query();
            },
            query :function(){
                var $this = this;
                var url = basepath+"/api/flightPlane/mySchedule?startDate="+this.startDate+"&endDate="+this.endDate
                axios.get(url).then(function (response) {
                   $this.tableData=[];
                   response.data.forEach(function (data) {
                       var map = {};
                       map.groundServiceScheduling=data;
                       map.flightPlane =map.groundServiceScheduling.flightPlanePo;
                       map.groundServiceScheduling.groundServiceSchedulings={id:map.groundServiceScheduling.GroundServiceSchedulingsId};
                       if(map.groundServiceScheduling&&map.groundServiceScheduling.selectedFwUser.length>1){
                           map.assistantUser=[];
                           map.groundServiceScheduling.selectedFwUser.forEach(function (data) {
                               if(data.main){
                                   map.mainUser = data;
                               }else{
                                   map.assistantUser.push(data);
                               }
                           })
                       }else{
                           map.mainUser = map.groundServiceScheduling.selectedFwUser[0];
                       }

                       $this.tableData.push(map);
                   })


               });
            },
            nodeCompleted:function (node,nodes) {
                if(nodes){
                    var result= false;
                    nodes.forEach(function (value) {
                        if( node.nodeName == value.nodeName){
                            result = true;
                        }
                    })
                    return result;
                }
                return false;

            },
            schedulingReceive:function (schedule,node) {
                var $this = this;
                this.openAlert = true;
                this.dialogTitle="确定 "+node.nodeName+" 节点已完成"
                node.flightPlanePo = JSON.parse(JSON.stringify(schedule.flightPlane));
                node.flightPlanePo.courseNodePos=[];
                node.dataSources="PMD";
                this.currentDialogM = function () {
                    this.openAlert = false;
                    axios.post(basepath+"/api/flightPlane/nodeCompleted",node).then(function (response) {
                        $this.openNormalSnackbar(node.nodeName+" 节点已完成");
                        if(!schedule.flightPlane.courseNodePos){

                            schedule.flightPlane.courseNodePos=[];
                        }
                        schedule.flightPlane.courseNodePos.push(response.data);
                    });
                }
            },
            schedulingFinish:function (scope) {
                this.openAlert = true;
                this.dialogTitle="确定要结束任务？"
                var $this = this;
                this.currentDialogM = function () {
                    this.openAlert = false;
                    scope.groundServiceScheduling.status="任务结束";
                    axios.post(basepath+"/api/flightPlane/schedulingFinish",scope.groundServiceScheduling).then(function (data) {
                        $this.openNormalSnackbar("任务已结束");
                    });
                }
            },
            dialogConfirm:function () {
                this.currentDialogM();
            },
            openNormalSnackbar:function (message) {
                if (this.normal.timer)
                {
                    clearTimeout(this.normal.timer);
                }
                this.normal.open = true;
                this.normal.message = message;
                var $this = this;
                this.normal.timer = setTimeout(function(){
                    $this.normal.open = false;
                }, $this.normal.timeout);
            }
        },
        mounted:function() {
           this.today();
           var $this=this;

            axios.get(basepath+"/api/sysSetting/findCnb"
            ).then(function (response) {
                $this.nodes=response.data;
            });
        }
        ,data:function(){
            return {
                currentDialogM:function () {

                },
                nodes:[],
                rights:this.$route.params.rights,
                openAlert:false,
                currentDate:new moment(),
                startDate:"",
                endDate:"",
                dialogTitle:'',
                normal:{switch:false},
                tableData:[],
                normal: {
                    message: 'Hello World, Snackbar !',
                    open: false,
                    timeout: 2000,
                    timeDiff:false
                }

            }
        },
        created:function (){
            messages.$emit('change','我的任务');
        }
    });
    module.exports = VueComponent;
})
